﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using UpdaterForm.Annotations;

namespace UpdaterForm.Tabs
{
    /// <summary>
    ///     Interaction logic for NewAppTab.xaml
    /// </summary>
    public partial class AddApp : INotifyPropertyChanged
    {
        private bool _pathExists;

        public AddApp()
        {
            InitializeComponent();
        }

        private String _installationPath { get; set; }
        public String InstallationPath
        {
            get { return _installationPath; }
            set
            {
                _installationPath = value;
                OnPropertyChanged("InstallationPath");
            }
        }

        public bool PathExists
        {
            get { return _pathExists; }
            set
            {
                _pathExists = value;
                OnPropertyChanged("PathExists");
            }
        }

        public event EventHandler OnAdd;

      

        //TODO: path selecting appeard in repo selecting - refactor move to util class
        private void SelectFolder(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog
            {
                Description = "Select application installation path."
            };
            DialogResult result = dialog.ShowDialog();
            if (result != DialogResult.OK)
                return;
            InstallationPath = dialog.SelectedPath;
            PathExists = Directory.Exists(InstallationPath);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var dir = new DirectoryInfo(InstallationPath);
            Settings.Instance.Data.Add(new AppData
            {
                AppName = dir.Name,
                CurrentVersion = "0",
                InstallationPath = InstallationPath
            });
            if (OnAdd != null)
                OnAdd(null, null);
        }  
        
        public event PropertyChangedEventHandler PropertyChanged;
        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}